package testCases;

import common.InitAndClose;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

// 注册和登录测试用例
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class RegisterAndLoginTest extends InitAndClose {

    // 注册页面
    @Order(1)
    @ParameterizedTest
    @CsvFileSource(resources = "regTestCases.csv")
    public void regTest(String username, String password) throws InterruptedException {
        webDriver.get("http://8.130.172.144:8081/register.html");
        // 等待页面加载完成
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        // 输入账号
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username == null ? "" : username);
        // 输入密码
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password == null ? "" : password);
        // 点击注册按钮
        webDriver.findElement(By.cssSelector("#submit")).click();

        // 等待 alert 框出现
        WebDriverWait wait = new WebDriverWait(webDriver, 5);
        // 获取 alert
        Alert alert = null;
        try {
            alert = wait.until(ExpectedConditions.alertIsPresent());
        } catch (Exception e) {
            System.out.println("测试不通过, 没有 alert 弹窗");
            Assertions.fail();
        }
        String alertText = alert.getText();
        // 点击确定
        alert.accept();
        if (alertText.equals("注册成功! 将跳转到登录页面!")) {
            // 注册成功
            webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
            String url = "http://8.130.172.144:8081/login.html";
            String currentUrl = webDriver.getCurrentUrl();
            Assertions.assertEquals(url, currentUrl);
            System.out.println("测试通过");
            System.out.println("alert: " + alertText);
        } else {
            // 失败
            System.out.println("测试不通过");
            System.out.println("alert: " + alertText);
            Assertions.fail();
        }
    }

    @Order(2)
    @Test
    public void regTestEnter() throws InterruptedException {
        String username = "zhaoliu";
        String password = "123456";
        webDriver.get("http://8.130.172.144:8081/register.html");
        // 等待页面加载完成
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        // 输入账号
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        // 输入密码并按下回车
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password, Keys.ENTER);
        // 显示等待 alert 并获取
        WebDriverWait wait = new WebDriverWait(webDriver, 5);
        try {
            Alert alert = wait.until(ExpectedConditions.alertIsPresent());
            alert.accept();
        } catch (Exception e) {
            System.out.println("enter 键测试不通过");
            Assertions.fail();
        }
        System.out.println("enter 键测试通过");
    }

    // 登录页面
    @Order(3)
    @ParameterizedTest
    @CsvFileSource(resources = "loginTestCases.csv")
    public void loginTest(String username, String password) {
        webDriver.get("http://8.130.172.144:8081/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);

        // 输入账号密码
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username == null ? "" : username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password == null ? "" : password);

        // 鼠标点击登录
        webDriver.findElement(By.cssSelector("#submit")).click();

        // 判断是否有弹窗
        WebDriverWait wait = new WebDriverWait(webDriver, 3);
        try {
            Alert alert = wait.until(ExpectedConditions.alertIsPresent());
            String alertText = alert.getText();
            alert.accept();
            Assertions.fail(alertText);
        } catch (Exception e) {
            System.out.println("无弹窗, 即将跳转页面, 测试继续");
        }

        // 判断是否到了会话页面
        String url = "http://8.130.172.144:8081/client.html";
        // 等待页面加载完毕
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
        String currentUrl = webDriver.getCurrentUrl();
        Assertions.assertEquals(url, currentUrl);

        // 判断左上角用户名是否与登录时的用户名一致
        String user = webDriver.findElement(By.cssSelector("body > div.client-container > div > div.left > div.user"))
                .getText();

        try {
            Assertions.assertEquals(user, username);
            System.out.println("登录测试通过, 当前用户名: " + user);
        } catch (Exception e) {
            System.out.println("测试不通过, \n登录时用户名: " + username + "\n左上角用户名: " + user);
        }
    }

    // enter 键测试
    @Order(4)
    @Test
    public void loginTestEnter() {
        webDriver.get("http://8.130.172.144:8081/login.html");
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);

        String username = "zhangsan";
        String password = "123456";

        // 输入账号密码后并按下回车
        webDriver.findElement(By.cssSelector("#username")).sendKeys(username);
        webDriver.findElement(By.cssSelector("#password")).sendKeys(password, Keys.ENTER);

        // 判断当前页面 url 跟登录页是否一致
        String currentUrl = webDriver.getCurrentUrl();
        try {
            Assertions.assertEquals("http://8.130.172.144:8081/login.html", currentUrl);
            System.out.println("enter 键测试通过");
        } catch (Exception e) {
            System.out.println("enter 键测试不通过");
        }
    }
}
